// pages/goods/exchange/index.js
const app = getApp()
import Toast from "../../../vant/weapp/toast/toast";
import clickreply from "../../../utils/clickreply.js";
Page({
  data: {
    address: [],
    isAddress: false,
    addressShow: false,
    detail: []
  },

  onLoad(options) {
    const that = this
    app.http.post(app.api.goodsOrderDetail, {
      order_on: options.order_on
    }, (res) => {
      if (res.data.order.address_lan == "") {
        that.setData({
          addressShow: false,
          isAddress: true,
          detail: res.data.order
        })
        return
      }
      let address = {
        realname: res.data.order.realname,
        mobile: res.data.order.mobile,
        address_name: res.data.order.address_lan
      }
      wx.setStorageSync('address', {
        id: 1,
        address,
      })
      that.setData({
        addressShow: true,
        address,
        isAddress: true,
        detail: res.data.order
      })


    })
  },
  onShow() {

    const that = this
    if (wx.getStorageSync('address').id !== 0) {
      let address = wx.getStorageSync('address')
      that.setData({
        addressShow: true,
        address,
        isAddress: true,
      })
      if (wx.getStorageSync('add') == 1) { //更换地址返回，调用更换地址接口

           if (!that.data.detail.order_on) {
            this.choose()
      }
      that.getAddressList()
console.log(that.data.detail.order_on)
        that.goodsUpdateOrder({
          address_id: wx.getStorageSync('address').id,
          order_on: that.data.detail.order_on
        })
      }
    } else {
      if (!that.data.detail.order_on) {
        return
      }
      that.getAddressList()
    }
  },
  getWaChat(e) {
    const that = this
    console.log(e)
    wx.login({
      success(codes) {
        if (codes.code) {
          app.http.post(app.api.wechat, {
            order_on: e.order_on,
            code: codes.code,
            type: e.type
          }, (re) => {
            console.log(re)
            wx.requestPayment({
              appid: re.data.appId,
              timeStamp: re.data.timeStamp,
              nonceStr: re.data.nonceStr,
              package: re.data.package,
              signType: re.data.signType,
              paySign: re.data.paySign,
              success(da) {
                if (da.errMsg == 'requestPayment:ok') {
                  wx.showModal({
                    title: '成功',
                    content: "兑换成功",
                    showCancel: false,
                    complete: (res) => {
                      if (res.confirm) {
                        wx.reLaunch({
                          url: '/pages/person/record/index',
                        })
                      }
                    }
                  })

                }
              },
              fail() {
                Toast({
                  type: 'fail',
                  message: '预定失败',
                  onClose: () => {

                  },
                });
              }
            })
          })

        }
      }
    })
  },
  goodsUpdateOrder(e) {
    console.log(e)
    app.http.post(app.api.goodsUpdateOrder, {
      address_id: e.address_id,
      order_on: e.order_on
    }, (res) => {
      Toast.clear()
    })
  },
  buy() {
    if (clickreply.preventDuplicateClicks(4)) return;
    const that = this
    if (wx.getStorageSync('address').id === 0) {
      Toast.fail("请选择收货地址")
      return
    }
    if (this.data.detail.order_money > 0) {
      that.getWaChat({
        order_on: that.data.detail.order_on,
        type: 5
      })
    } else {
      app.http.post(app.api.goodsAddOrder, {
        order_on: this.data.detail.order_on
      }, (res) => {
        if (res.code == 200) {
          setTimeout(() => {
            Toast({
              type: 'success',
              message: "兑换成功",
              onClose: () => {
                setTimeout(() => {
                  wx.reLaunch({
                    url: '/pages/person/record/index',
                  })
                }, 2000)
              },
            });
          }, 0)


        }
      })
    }

  },
  getAddressList() {
    const that = this
    app.http.get(app.api.addressList, {}, (res) => {
      if (res.data.length > 0) {

        if (wx.getStorageSync('add') == 1) {  //唯一地址为新建地址返回，调用更换地址接口
          let address = res.data[0]
          wx.setStorageSync('address', res.data[0])
          that.goodsUpdateOrder({
            address_id: res.data[0].id,
            order_on: that.data.detail.order_on
          })
          that.setData({
            addressShow: true,
            address,
            isAddress: true,
          })
        } else {
          that.setData({
            addressShow: false,
            isAddress: true,
          })
        }
      }

    })
  },
  choose() {
    if (this.data.isAddress) {
      wx.navigateTo({
        url: "/pages/person/address/index?type=1",
      });
    } else {
      wx.navigateTo({
        url: "/pages/person/addressAdd/index?type=2",
      });
    }
  },
});